#!/usr/bin/env python
# USP Notes Plugin Version 1.0
##Description:A simple quick notes cut/paste pad
import gtk
import gtk.glade
#import sys
import os
import gobject
import datetime
import gconf
#import fcntl
from execute import *
from easygconf import *

class pluginclass:
	TARGET_TYPE_TEXT = 80
	toButton = [ ( "text/uri-list", 0, TARGET_TYPE_TEXT ) ]
	"""This is the main class for the plugin"""
	"""It MUST be named pluginclass"""
	def __init__(self, USPWin):

		self.USPWin = USPWin

		#The Glade file for the plugin
		self.gladefile = os.path.join(os.path.dirname(__file__), "notes.glade")

		#Read GLADE file
		self.wTree = gtk.glade.XML(self.gladefile,"window1")

		#These properties are NECESSARY to maintain consistency
		#throughout USP

		#Set 'window' property for the plugin (Must be the root widget)
		self.window = self.wTree.get_widget("window1")

		#Set 'heading' property for plugin
		self.heading = "Quick Notes"

		#This should be the first item added to the window in glade
		self.content_holder = self.wTree.get_widget("eventbox1")

		#Plugin icon
		self.icon = 'gedit-icon.png'

		self.gconf_dir = '/apps/usp/plugins/notes'
		self.client = gconf.client_get_default()
		self.client.add_dir('/apps/usp/plugins/notes', gconf.CLIENT_PRELOAD_NONE)
		self.client.notify_add('/apps/usp/plugins/notes/height',self.RegenPlugins)
		self.client.notify_add('/apps/usp/plugins/notes/width',self.RegenPlugins)
		self.client.notify_add('/apps/usp/plugins/notes/sticky',self.RegenPlugins)
		self.client.notify_add('/apps/usp/plugins/notes/icon',self.RegenPlugins)

		self.wTree.get_widget("SaveBtn").set_tooltip_text( "Save" )
		self.wTree.get_widget("ClrBtn").set_tooltip_text( "Clear" )
		self.RegenPlugins()
		#gobject.timeout_add(1000, self.DoNotes)

		#Connect event handlers
		dic = { "on_window1_destroy" : gtk.main_quit,
				"on_SaveBtn_clicked" : self.saveit,
				"on_ClrBtn_clicked" : self.clearit}
		self.wTree.signal_autoconnect(dic)

	def RegenPlugins(self, *args, **kargs):
		self.GetGconfEntries()
		self.DoNotes

	def GetGconfEntries(self):
		self.client = gconf.client_get_default()
		self.height = SetGconf(self.client,'int','/apps/usp/plugins/notes/height',100)
		self.width = SetGconf(self.client,'int','/apps/usp/plugins/notes/width', 200)
		self.minimized = SetGconf( self.client, "bool", "/apps/usp/plugins/notes/minimized", False )
		self.sticky = SetGconf( self.client, "bool", "/apps/usp/plugins/notes/sticky", False )
		self.icon = SetGconf( self.client, "string", "/apps/usp/plugins/notes/icon", "gedit-icon.png" )
		self.savepath = SetGconf( self.client, "string", "/apps/usp/plugins/notes/save_path", "" )

	def SetHidden( self, state ):
		if state == True:
			WriteGconf( self.client, "bool", "/apps/usp/plugins/notes/minimized", True )
		else:
			WriteGconf( self.client, "bool", "/apps/usp/plugins/notes/minimized", False )

	def saveit(self, *args, **kargs):
		os.chdir(os.path.expanduser("~"))
		tbuffer = self.wTree.get_widget("TextPad").get_buffer()
		if tbuffer.get_text(tbuffer.get_start_iter(),tbuffer.get_end_iter(),include_hidden_chars=False) != "":
			if self.savepath != "" and os.path.exists(self.savepath):
				if self.savepath[-1:] != "/":
					self.savepath = self.savepath + "/"
				filestr=self.savepath+self.wTree.get_widget("FileEntry").get_text()+datetime.datetime.today().strftime("%Y%m%d-%H:%M:%S")
			else:
				filestr=os.path.expanduser("~")+"/Desktop/"+self.wTree.get_widget("FileEntry").get_text()+datetime.datetime.today().strftime("%Y%m%d-%H:%M:%S")
			out_file = open(filestr,"w")
			out_file.write(tbuffer.get_text(tbuffer.get_start_iter(),tbuffer.get_end_iter(),include_hidden_chars=False))
			out_file.close()

	def clearit(self, *args, **kargs):
		tbuffer = self.wTree.get_widget("TextPad").get_buffer()
		tbuffer.set_text("")
		self.savepath = SetGconf( self.client, "string", "/apps/usp/plugins/notes/save_path", "" )
		self.wTree.get_widget("FileEntry").set_text("")

	def DoNotes(self, *args, **kargs):
		self.wTree.get_widget("TextPad").set_size_request(self.width-10,self.height-34)
		self.wTree.get_widget("vbox1").set_size_request(self.width,self.height)

#---------------------------------------------------------------------------------------#
# USPconfig Section Below
#---------------------------------------------------------------------------------------#
# This is the Config Class it must be called "cfgpluginclass'
class cfgpluginclass:
	def __init__(self):
		# The Gladefile for the plugins USPconfig Tab
		self.gladefile = os.path.join(os.path.dirname(__file__), "notes.glade")
		# Read GLADE file
		self.wTree = gtk.glade.XML(self.gladefile,"config")
		# Set 'window' property for the plugin (Must be the root widget)
		self.window = self.wTree.get_widget("config")
		# Set Heading, this will be used for the tab label in USPconfig
		self.heading = "Notes"
		# Content Place Holder must be eventbox1
		self.content_holder = self.wTree.get_widget("eventbox2")
		# GConf Stuff - This just makes sure a gconf path is there.
		self.gconf_dir = '/apps/usp/plugins/notes'
		self.client = gconf.client_get_default()
		self.client.add_dir('/apps/usp/plugins/notes', gconf.CLIENT_PRELOAD_NONE)
		# Setup the Functions of the Glade files spin controls or entry boxes for Value Changes
		# Tip: For spinbutton controls use the "_value_changed" event not the "_changed" event.
		dic = { "on_window1_destroy" : gtk.main_quit,
				"on_NotesWSpin_changed" : self.notesw,
				"on_NotesHSpin_changed" : self.notesh,
				"on_NotesIconEntry_changed" : self.notesiconchange,
				"on_NotesPathEntry_changed" : self.notespathchange,
				"on_StickyChkBtn_toggled" : self.notessticky}
		self.wTree.signal_autoconnect(dic)
		# Read Values from Gconf and Set Default Values if they don't exist.
		self.wTree.get_widget("NotesWSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/notes/width',200))
		self.wTree.get_widget("NotesHSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/notes/height',200))
		self.wTree.get_widget("StickyChkBtn").set_active(SetGconf(self.client,'bool','/apps/usp/plugins/notes/sticky',False))
		self.wTree.get_widget("NotesIconEntry").set_text(SetGconf(self.client,'string','/apps/usp/plugins/notes/icon','gedit-icon.png'))
		self.wTree.get_widget("NotesPathEntry").set_text(SetGconf(self.client,'string','/apps/usp/plugins/notes/save_path',''))

	# Functions for Changing Values when items checked or altered Section
	def notesw(self, widget, **kargs):
		self.client.set_int('/apps/usp/plugins/notes/width',int(self.wTree.get_widget("NotesWSpin").get_value()))

	def notesh(self, widget, **kargs):
		self.client.set_int('/apps/usp/plugins/notes/height',int(self.wTree.get_widget("NotesHSpin").get_value()))

	def notesiconchange(self, widget, **kargs): 
		self.icon=self.wTree.get_widget("NotesIconEntry").get_text()
		self.client.set_string('/apps/usp/plugins/notes/icon',self.wTree.get_widget('NotesIconEntry').get_text())

	def notespathchange(self, widget, **kargs): 
		self.icon=self.wTree.get_widget("NotesPathEntry").get_text()
		self.client.set_string('/apps/usp/plugins/notes/save_path',self.wTree.get_widget('NotesPathEntry').get_text())

	def notessticky(self, widget, **kargs):
		if self.wTree.get_widget("StickyChkBtn").get_active() == True:
			self.client.set_bool('/apps/usp/plugins/notes/sticky',True)
		else:
			self.client.set_bool('/apps/usp/plugins/notes/sticky',False)
